/*
 * Copyright: (C) 2014 EAST fulinux <fulinux@sina.com>
 */
#include <stdio.h>
#include <stdlib.h>
#include <linked_list.h>

struct student{
    int no;
    int age;
    int addr;
    struct dlist_node list;
};

/*
 * 主函数 
 */
int main (int argc, char **argv)
{
    int no;
    int nr = 0;
    int addr = 0;
    int flag = 0;
    struct student *p;
    struct dlist_node head;
    struct dlist_node *iter = NULL;
    struct dlist_node *iter_backup = NULL;

    list_init_head(&head);

    for(;;){
        if(scanf("%d", &no) == EOF){
            printf("input error\n");
            continue;
        }

        if(nr == 0 && list_is_empty(&head)){
            p	= (struct student *)malloc (sizeof(struct student));
            if(p==NULL) {
                fprintf( stderr, "\ndynamic memory allocation failed\n" );
                exit (EXIT_FAILURE);
            }
            
            p->no = no;
            p->addr = 100;
            list_push_back(&head, &p->list);
            nr++;
            printf("add no = %d, nr = %d, addr = %d\n", no, nr, p->addr);
        }else{
            if(iter == &head){
                list_for_each(iter, &head){
                    p = list_entry(iter, struct student, list);
                    if(p->no == no){
                        break;
                    }
                }

                if(iter == &head){ /*  如果相等说明有新模块添加 */
                    addr = 100;
                    list_for_each(iter, &head){
                        p = list_entry(iter, struct student, list);
                        if(p->addr == addr){
                            addr++;
                            iter = &head;
                        }
                    }

                    p = (struct student *)malloc (sizeof(struct student));
                    if(p==NULL) {
                        fprintf( stderr, "\ndynamic memory allocation failed\n" );
                        exit (EXIT_FAILURE);
                    }

                    p->no = no;
                    p->addr = addr;
                    list_push_back(&head, &p->list);
                    nr++;
                    printf("add no = %d, nr = %d, addr = %d\n", no, nr, addr);
                }
            }
        }

        if(flag == 0 && nr > 5){
            flag = 1;
            list_for_each(iter, &head){
                p = list_entry(iter, struct student, list);
                if(p->no == 3
                        || p->no == 1){
                    iter_backup = iter->next;
                    list_remove_node(iter);
                    iter = iter_backup;
                    printf("remove list_node no = %d\n", p->no);
                    nr--;
                }
            }
        }

        printf("==============================================\n");
        list_for_each(iter, &head){
            p = list_entry(iter, struct student, list);
            printf("have no = %d, addr = %d\n", p->no, p->addr);
        }
        printf("==============================================\n");
    }

    list_for_each_remove(iter, iter_backup, &head){
        p = list_entry(iter, struct student, list);
        list_remove_node(iter);
        free(p);
    }

    return 0;
} /* ----- End of main() ----- */
